import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Dialogs 1.2
import "base"

Item {
    ListModel {
        id: listModel
        ListElement { targetPCL: "点击选择文件"; measurePCL: "点击选择文件" }
    }
    Rectangle {
        id: tableView
        width: parent.width
        height: 300
        color: Common.colorMain
        Row {
            id: header
            height: parent.height / 10
            width: parent.width
            anchors.top: parent.top
            anchors.left: parent.left
            spacing: 0
            Text {
                height: parent.height
                width: parent.width * 0.4
                text: "基准点云"
                color: "white"
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.family: Common.fontMain
                font.pixelSize: 14
            }
            Rectangle {
                width: 1
                height: parent.height - 2
                anchors.verticalCenter: parent.verticalCenter
                color: "white"
            }
            Text {
                height: parent.height
                width: parent.width * 0.4
                text: "实测点云"
                color: "white"
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.family: Common.fontMain
                font.pixelSize: 14
            }
            Rectangle {
                width: 1
                height: parent.height - 2
                anchors.verticalCenter: parent.verticalCenter
                color: "white"
            }
            Text {
                height: parent.height
                width: parent.width * 0.2
                text: "删除"
                color: "white"
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.family: Common.fontMain
                font.pixelSize: 14
            }
        }
        Rectangle {
            height: 1
            anchors.top: header.bottom
            width: parent.width - 2
            anchors.horizontalCenter: parent.horizontalCenter
            color: "white"
        }
        Rectangle {
            id: listArea
            anchors.top: header.bottom
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            anchors.margins: 1
            clip: true
            ListView {
                model: listModel
                boundsBehavior: Flickable.StopAtBounds
                anchors.fill: parent
                delegate: Rectangle {
                    color: index % 2 == 0 ? Common.colorBG : "white"
                    width: listArea.width
                    height: 100
                    Row {
                        anchors.fill: parent
                        Text {
                            height: parent.height
                            width: parent.width * 0.4
                            text: targetPCL
                            color: Common.colorMain
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
                            font.family: Common.fontMain
                            font.pixelSize: 14
                            font.underline: true
                            wrapMode: Text.WrapAnywhere
                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    fileDialogSingle.targetIndex = index
                                    fileDialogSingle.open()
                                }
                            }
                        }
                        Text {
                            height: parent.height
                            width: parent.width * 0.4
                            wrapMode: Text.WrapAnywhere
                            text: measurePCL
                            color: Common.colorMain
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
                            font.family: Common.fontMain
                            font.pixelSize: 14
                            font.underline: true
                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    fileDialogMulti.targetIndex = index
                                    fileDialogMulti.open()
                                }
                            }
                        }
                        Text {
                            height: parent.height
                            width: parent.width * 0.2
                            text: "删除"
                            color: Common.colorMain
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
                            font.family: Common.fontMain
                            font.pixelSize: 14
                            font.underline: true
                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    listModel.remove(index)
                                }
                            }
                        }
                    }
                }
                ScrollBar.vertical: ScrollBar {
                    id: vbar
                    width: 10
                    anchors.top: parent.top
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    contentItem: Rectangle {
                        radius: vbar.width / 4
                        color: Common.colorMain
                        opacity: vbar.active ? 1.0 : 0.0
                        Behavior on opacity{PropertyAnimation{duration: 200}}
                    }
                }
            }
        }
    }
    MyButton {
        id: addPcl
        anchors.top: tableView.bottom
        anchors.topMargin: 10
        anchors.left: tableView.left
        height: 35
        text: "添加一组点云数据"
        width: parent.width
        onClicked: listModel.append({"targetPCL": "点击选择文件", "measurePCL": "点击选择文件"})
    }

    Row {
        anchors.top: addPcl.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        spacing: 5
        MyButton {
            height: 35
            width: 35
            Image {
                source: "qrc:/resource/icon_setting.svg"
                anchors.fill: parent
                anchors.margins: 5
            }
            onClicked: {
                dialogLoader.setSource("qrc:/qml/RegistParam.qml")
            }
        }
        MyButton {
            height: 35
            width: 100
            text: "粗配准点云"
            onClicked: {
                ViewBridge.registPly(true)
            }
        }
        MyButton {
            height: 35
            width: 100
            text: "精配准点云"
        }
    }
    Row {
        anchors.top: addPcl.bottom
        anchors.topMargin: 10
        anchors.right: parent.right
        spacing: 5
        MyButton {
            height: 35
            width: 35
            Image {
                source: "qrc:/resource/icon_setting.svg"
                anchors.fill: parent
                anchors.margins: 5
            }
            onClicked: {
                dialogLoader.setSource("qrc:/qml/CutParam.qml")
            }
        }
        MyButton {
            height: 35
            width: 100
            text: "分割点云"
        }
    }

    FileDialog {
        id: fileDialogMulti
        title: "请选择实测点云文件"
        nameFilters: ["*.ply; *.txt"]
        property int targetIndex: -1
        selectMultiple: true
        modality: Qt.WindowModal
        onAccepted: {
            var selectedFiles = fileDialogMulti.fileUrls;
            if(selectedFiles.length > 0) {
                var totalText = ""
                for(var item in selectedFiles) {
                    totalText += selectedFiles[item].replace("file:///", "") + ";"
                }
                listModel.set(targetIndex, {"measurePCL": totalText})
                ViewBridge.readPlys(targetIndex, totalText)
            }
        }
        onRejected: {
            console.log("文件选择被取消");
        }
    }

    FileDialog {
        id: fileDialogSingle
        title: "请选择基准点云文件"
        nameFilters: ["*.ply; *.txt"]
        property int targetIndex: -1
        selectMultiple: false
        modality: Qt.WindowModal
        onAccepted: {
            var selectedFile = fileDialogSingle.fileUrl.toString().replace("file:///", "")
            listModel.set(targetIndex, {"targetPCL": selectedFile})
            ViewBridge.readPly(targetIndex, selectedFile)
        }
        onRejected: {
            console.log("文件选择被取消")
        }
    }

    Loader {
        id: dialogLoader
        asynchronous: false
    }
}
